home *** CD-ROM | disk | FTP | other *** search
/ Plug Into a Brand New World / Plug Into a Brand New World Version 1.3.BIN / KW-585S25 / Linux / usbserial.c < prev   
Text File  |  2002-02-13  |  47KB  |  1,431 lines

  1. /*
  2.  * USB Serial Converter driver
  3.  *
  4.  * Copyright (C) 1999, 2000 Greg Kroah-Hartman (greg@kroah.com)
  5.  * Copyright (c) 2000 Peter Berger (pberger@brimson.com)
  6.  * Copyright (c) 2000 Al Borchers (borchers@steinerpoint.com)
  7.  *
  8.  *    This program is free software; you can redistribute it and/or modify
  9.  *    it under the terms of the GNU General Public License as published by
  10.  *    the Free Software Foundation; either version 2 of the License, or
  11.  *    (at your option) any later version.
  12.  *
  13.  * This driver was originally based on the ACM driver by Armin Fuerst (which was 
  14.  * based on a driver by Brad Keryan)
  15.  *
  16.  * See Documentation/usb/usb-serial.txt for more information on using this driver
  17.  *
  18.  * 2001_02_05 gkh
  19.  *    Fixed buffer overflows bug with the generic serial driver.  Thanks to
  20.  *    Todd Squires <squirest@ct0.com> for fixing this.
  21.  *
  22.  * (01/10/2001) gkh
  23.  *    Fixed bug where the generic serial adaptor grabbed _any_ device that was
  24.  *    offered to it.
  25.  *
  26.  * (12/12/2000) gkh
  27.  *    Removed MOD_INC and MOD_DEC from poll and disconnect functions, and
  28.  *    moved them to the serial_open and serial_close functions.
  29.  *    Also fixed bug with there not being a MOD_DEC for the generic driver
  30.  *    (thanks to Gary Brubaker for finding this.)
  31.  *
  32.  * (11/29/2000) gkh
  33.  *    Small NULL pointer initialization cleanup which saves a bit of disk image
  34.  *
  35.  * (11/01/2000) Adam J. Richter
  36.  *    instead of using idVendor/idProduct pairs, usb serial drivers
  37.  *    now identify their hardware interest with usb_device_id tables,
  38.  *    which they usually have anyhow for use with MODULE_DEVICE_TABLE.
  39.  *
  40.  * (10/05/2000) gkh
  41.  *    Fixed bug with urb->dev not being set properly, now that the usb
  42.  *    core needs it.
  43.  * 
  44.  * (09/11/2000) gkh
  45.  *    Removed DEBUG #ifdefs with call to usb_serial_debug_data
  46.  *
  47.  * (08/28/2000) gkh
  48.  *    Added port_lock to port structure.
  49.  *    Added locks for SMP safeness to generic driver
  50.  *    Fixed the ability to open a generic device's port more than once.
  51.  *
  52.  * (07/23/2000) gkh
  53.  *    Added bulk_out_endpointAddress to port structure.
  54.  *
  55.  * (07/19/2000) gkh, pberger, and borchers
  56.  *    Modifications to allow usb-serial drivers to be modules.
  57.  *
  58.  * (07/03/2000) gkh
  59.  *    Added more debugging to serial_ioctl call
  60.  * 
  61.  * (06/25/2000) gkh
  62.  *    Changed generic_write_bulk_callback to not call wake_up_interruptible
  63.  *    directly, but to have port_softint do it at a safer time.
  64.  *
  65.  * (06/23/2000) gkh
  66.  *    Cleaned up debugging statements in a quest to find UHCI timeout bug.
  67.  *
  68.  * (05/22/2000) gkh
  69.  *    Changed the makefile, enabling the big CONFIG_USB_SERIAL_SOMTHING to be 
  70.  *    removed from the individual device source files.
  71.  *
  72.  * (05/03/2000) gkh
  73.  *    Added the Digi Acceleport driver from Al Borchers and Peter Berger.
  74.  * 
  75.  * (05/02/2000) gkh
  76.  *    Changed devfs and tty register code to work properly now. This was based on
  77.  *    the ACM driver changes by Vojtech Pavlik.
  78.  *
  79.  * (04/27/2000) Ryan VanderBijl
  80.  *     Put calls to *_paranoia_checks into one function.
  81.  * 
  82.  * (04/23/2000) gkh
  83.  *    Fixed bug that Randy Dunlap found for Generic devices with no bulk out ports.
  84.  *    Moved when the startup code printed out the devices that are supported.
  85.  *
  86.  * (04/19/2000) gkh
  87.  *    Added driver for ZyXEL omni.net lcd plus ISDN TA
  88.  *    Made startup info message specify which drivers were compiled in.
  89.  *
  90.  * (04/03/2000) gkh
  91.  *    Changed the probe process to remove the module unload races.
  92.  *    Changed where the tty layer gets initialized to have devfs work nicer.
  93.  *    Added initial devfs support.
  94.  *
  95.  * (03/26/2000) gkh
  96.  *    Split driver up into device specific pieces.
  97.  * 
  98.  * (03/19/2000) gkh
  99.  *    Fixed oops that could happen when device was removed while a program
  100.  *    was talking to the device.
  101.  *    Removed the static urbs and now all urbs are created and destroyed
  102.  *    dynamically.
  103.  *    Reworked the internal interface. Now everything is based on the 
  104.  *    usb_serial_port structure instead of the larger usb_serial structure.
  105.  *    This fixes the bug that a multiport device could not have more than
  106.  *    one port open at one time.
  107.  *
  108.  * (03/17/2000) gkh
  109.  *    Added config option for debugging messages.
  110.  *    Added patch for keyspan pda from Brian Warner.
  111.  *
  112.  * (03/06/2000) gkh
  113.  *    Added the keyspan pda code from Brian Warner <warner@lothar.com>
  114.  *    Moved a bunch of the port specific stuff into its own structure. This
  115.  *    is in anticipation of the true multiport devices (there's a bug if you
  116.  *    try to access more than one port of any multiport device right now)
  117.  *
  118.  * (02/21/2000) gkh
  119.  *    Made it so that any serial devices only have to specify which functions
  120.  *    they want to overload from the generic function calls (great, 
  121.  *    inheritance in C, in a driver, just what I wanted...)
  122.  *    Added support for set_termios and ioctl function calls. No drivers take
  123.  *    advantage of this yet.
  124.  *    Removed the #ifdef MODULE, now there is no module specific code.
  125.  *    Cleaned up a few comments in usb-serial.h that were wrong (thanks again
  126.  *    to Miles Lott).
  127.  *    Small fix to get_free_serial.
  128.  *
  129.  * (02/14/2000) gkh
  130.  *    Removed the Belkin and Peracom functionality from the driver due to
  131.  *    the lack of support from the vendor, and me not wanting people to 
  132.  *    accidenatly buy the device, expecting it to work with Linux.
  133.  *    Added read_bulk_callback and write_bulk_callback to the type structure
  134.  *    for the needs of the FTDI and WhiteHEAT driver.
  135.  *    Changed all reverences to FTDI to FTDI_SIO at the request of Bill
  136.  *    Ryder.
  137.  *    Changed the output urb size back to the max endpoint size to make
  138.  *    the ftdi_sio driver have it easier, and due to the fact that it didn't
  139.  *    really increase the speed any.
  140.  *
  141.  * (02/11/2000) gkh
  142.  *    Added VISOR_FUNCTION_CONSOLE to the visor startup function. This was a
  143.  *    patch from Miles Lott (milos@insync.net).
  144.  *    Fixed bug with not restoring the minor range that a device grabs, if
  145.  *    the startup function fails (thanks Miles for finding this).
  146.  *
  147.  * (02/05/2000) gkh
  148.  *    Added initial framework for the Keyspan PDA serial converter so that
  149.  *    Brian Warner has a place to put his code.
  150.  *    Made the ezusb specific functions generic enough that different
  151.  *    devices can use them (whiteheat and keyspan_pda both need them).
  152.  *    Split out a whole bunch of structure and other stuff to a seperate
  153.  *    usb-serial.h file.
  154.  *    Made the Visor connection messages a little more understandable, now
  155.  *    that Miles Lott (milos@insync.net) has gotten the Generic channel to
  156.  *    work. Also made them always show up in the log file.
  157.  * 
  158.  * (01/25/2000) gkh
  159.  *    Added initial framework for FTDI serial converter so that Bill Ryder
  160.  *    has a place to put his code.
  161.  *    Added the vendor specific info from Handspring. Now we can print out
  162.  *    informational debug messages as well as understand what is happening.
  163.  *
  164.  * (01/23/2000) gkh
  165.  *    Fixed problem of crash when trying to open a port that didn't have a
  166.  *    device assigned to it. Made the minor node finding a little smarter,
  167.  *    now it looks to find a continous space for the new device.
  168.  *
  169.  * (01/21/2000) gkh
  170.  *    Fixed bug in visor_startup with patch from Miles Lott (milos@insync.net)
  171.  *    Fixed get_serial_by_minor which was all messed up for multi port 
  172.  *    devices. Fixed multi port problem for generic devices. Now the number
  173.  *    of ports is determined by the number of bulk out endpoints for the
  174.  *    generic device.
  175.  *
  176.  * (01/19/2000) gkh
  177.  *    Removed lots of cruft that was around from the old (pre urb) driver 
  178.  *    interface.
  179.  *    Made the serial_table dynamic. This should save lots of memory when
  180.  *    the number of minor nodes goes up to 256.
  181.  *    Added initial support for devices that have more than one port. 
  182.  *    Added more debugging comments for the Visor, and added a needed 
  183.  *    set_configuration call.
  184.  *
  185.  * (01/17/2000) gkh
  186.  *    Fixed the WhiteHEAT firmware (my processing tool had a bug)
  187.  *    and added new debug loader firmware for it.
  188.  *    Removed the put_char function as it isn't really needed.
  189.  *    Added visor startup commands as found by the Win98 dump.
  190.  * 
  191.  * (01/13/2000) gkh
  192.  *    Fixed the vendor id for the generic driver to the one I meant it to be.
  193.  *
  194.  * (01/12/2000) gkh
  195.  *    Forget the version numbering...that's pretty useless...
  196.  *    Made the driver able to be compiled so that the user can select which
  197.  *    converter they want to use. This allows people who only want the Visor
  198.  *    support to not pay the memory size price of the WhiteHEAT.
  199.  *    Fixed bug where the generic driver (idVendor=0000 and idProduct=0000)
  200.  *    grabbed the root hub. Not good.
  201.  * 
  202.  * version 0.4.0 (01/10/2000) gkh
  203.  *    Added whiteheat.h containing the firmware for the ConnectTech WhiteHEAT
  204.  *    device. Added startup function to allow firmware to be downloaded to
  205.  *    a device if it needs to be.
  206.  *    Added firmware download logic to the WhiteHEAT device.
  207.  *    Started to add #defines to split up the different drivers for potential
  208.  *    configuration option.
  209.  *    
  210.  * version 0.3.1 (12/30/99) gkh
  211.  *      Fixed problems with urb for bulk out.
  212.  *      Added initial support for multiple sets of endpoints. This enables
  213.  *      the Handspring Visor to be attached successfully. Only the first
  214.  *      bulk in / bulk out endpoint pair is being used right now.
  215.  *
  216.  * version 0.3.0 (12/27/99) gkh
  217.  *    Added initial support for the Handspring Visor based on a patch from
  218.  *    Miles Lott (milos@sneety.insync.net)
  219.  *    Cleaned up the code a bunch and converted over to using urbs only.
  220.  *
  221.  * version 0.2.3 (12/21/99) gkh
  222.  *    Added initial support for the Connect Tech WhiteHEAT converter.
  223.  *    Incremented the number of ports in expectation of getting the
  224.  *    WhiteHEAT to work properly (4 ports per connection).
  225.  *    Added notification on insertion and removal of what port the
  226.  *    device is/was connected to (and what kind of device it was).
  227.  *
  228.  * version 0.2.2 (12/16/99) gkh
  229.  *    Changed major number to the new allocated number. We're legal now!
  230.  *
  231.  * version 0.2.1 (12/14/99) gkh
  232.  *    Fixed bug that happens when device node is opened when there isn't a
  233.  *    device attached to it. Thanks to marek@webdesign.no for noticing this.
  234.  *
  235.  * version 0.2.0 (11/10/99) gkh
  236.  *    Split up internals to make it easier to add different types of serial 
  237.  *    converters to the code.
  238.  *    Added a "generic" driver that gets it's vendor and product id
  239.  *    from when the module is loaded. Thanks to David E. Nelson (dnelson@jump.net)
  240.  *    for the idea and sample code (from the usb scanner driver.)
  241.  *    Cleared up any licensing questions by releasing it under the GNU GPL.
  242.  *
  243.  * version 0.1.2 (10/25/99) gkh
  244.  *     Fixed bug in detecting device.
  245.  *
  246.  * version 0.1.1 (10/05/99) gkh
  247.  *     Changed the major number to not conflict with anything else.
  248.  *
  249.  * version 0.1 (09/28/99) gkh
  250.  *     Can recognize the two different devices and start up a read from
  251.  *    device when asked to. Writes also work. No control signals yet, this
  252.  *    all is vendor specific data (i.e. no spec), also no control for
  253.  *    different baud rates or other bit settings.
  254.  *    Currently we are using the same devid as the acm driver. This needs
  255.  *    to change.
  256.  * 
  257.  */
  258. #include <linux/config.h>
  259. #include <linux/kernel.h>
  260. #include <linux/sched.h>
  261. #include <linux/signal.h>
  262. #include <linux/errno.h>
  263. #include <linux/poll.h>
  264. #include <linux/init.h>
  265. #include <linux/slab.h>
  266. #include <linux/fcntl.h>
  267. #include <linux/tty.h>
  268. #include <linux/tty_driver.h>
  269. #include <linux/tty_flip.h>
  270. #include <linux/module.h>
  271. #include <linux/spinlock.h>
  272. #include <linux/list.h>
  273. #include <linux/smp_lock.h>
  274.  
  275. #ifdef USB_SERIAL_DEBUG
  276. #define DEBUG
  277. #else
  278. #undef DEBUG
  279. #endif
  280. #include <linux/usb.h>
  281.  
  282. /* Module information */
  283. MODULE_AUTHOR("Greg Kroah-Hartman, greg@kroah.com, http://www.kroah.com/linux-usb/");
  284. MODULE_DESCRIPTION("USB Serial Driver");
  285.  
  286. #include "usb-serial.h"
  287.  
  288. #define MAX(a, b) (((a) > (b)) ? (a) : (b))
  289. /* function prototypes for a "generic" type serial converter (no flow control, not all endpoints needed) */
  290.  
  291. /* need to always compile these in, as some of the other devices use these functions as their own. */
  292.  
  293. /* if a driver does not provide a function pointer, the generic function will be called. */
  294. static int    generic_open(struct usb_serial_port *port, struct file *filp);
  295. static void   generic_close(struct usb_serial_port *port, struct file *filp);
  296. static int    generic_write(struct usb_serial_port *port, int from_user, const unsigned char *buf, int count);
  297. static int    generic_write_room(struct usb_serial_port *port);
  298. static int    generic_chars_in_buffer(struct usb_serial_port *port);
  299. static void   generic_read_bulk_callback(struct urb *urb);
  300. static void   generic_write_bulk_callback(struct urb *urb);
  301.  
  302. #ifdef CONFIG_USB_SERIAL_GENERIC
  303. static void   generic_shutdown(struct usb_serial *serial);
  304. static __u16  vendor = 0x05f9;
  305. static __u16  product = 0xffff;
  306. MODULE_PARM(vendor, "i");
  307. MODULE_PARM_DESC(vendor, "User specified USB idVendor");
  308.  
  309. MODULE_PARM(product, "i");
  310. MODULE_PARM_DESC(product, "User specified USB idProduct");
  311.  
  312. static struct usb_device_id           generic_device_ids[2];  /* Initially all zeroes. */
  313.  
  314. /* All of the device info needed for the Generic Serial Converter */
  315. static struct usb_serial_device_type  generic_device =
  316. {
  317. name:
  318.   "Generic",
  319.   id_table : generic_device_ids,
  320.   needs_interrupt_in : DONT_CARE, /* don't have to have an interrupt in endpoint */
  321.   needs_bulk_in : DONT_CARE,      /* don't have to have a bulk in endpoint */
  322.   needs_bulk_out : DONT_CARE,     /* don't have to have a bulk out endpoint */
  323.   num_interrupt_in : NUM_DONT_CARE,
  324.   num_bulk_in : NUM_DONT_CARE,
  325.   num_bulk_out : NUM_DONT_CARE,
  326.   num_ports : 1,
  327.   shutdown : generic_shutdown,
  328. };
  329. #endif
  330.  
  331. /* local function prototypes */
  332. static int                            serial_open(struct tty_struct *tty, struct file *filp);
  333. static void                           serial_close(struct tty_struct *tty, struct file *filp);
  334. static int                            serial_write(struct tty_struct   *tty, int                 from_user,
  335.                                                    const unsigned char *buf, int                 count);
  336. static int                            serial_write_room(struct tty_struct *tty);
  337. static int                            serial_chars_in_buffer(struct tty_struct *tty);
  338. static void                           serial_throttle(struct tty_struct *tty);
  339. static void                           serial_unthrottle(struct tty_struct *tty);
  340. static int                            serial_ioctl(struct tty_struct *tty, struct file       *file, unsigned int      cmd,
  341.                                                    unsigned long     arg);
  342. static void                           serial_set_termios(struct tty_struct *tty, struct termios *old);
  343.  
  344. static void                           *usb_serial_probe(struct usb_device           *dev, unsigned int                ifnum,
  345.                                                         const struct usb_device_id  *id);
  346. static void                           usb_serial_disconnect(struct usb_device *dev, void *ptr);
  347.  
  348. static struct usb_driver              usb_serial_driver =
  349. {
  350. name:
  351.   "serial",
  352.   probe : usb_serial_probe,
  353.   disconnect : usb_serial_disconnect,
  354.   id_table : NULL,                /* check all devices */
  355. };
  356.  
  357. /* There is no MODULE_DEVICE_TABLE for usbserial.c.  Instead
  358.    the MODULE_DEVICE_TABLE declarations in each serial driver
  359.    cause the "hotplug" program to pull in whatever module is necessary
  360.    via modprobe, and modprobe will load usbserial because the serial
  361.    drivers depend on it.
  362. */
  363. static int                            serial_refcount;
  364. static struct tty_driver              serial_tty_driver;
  365. static struct tty_struct              *serial_tty[SERIAL_TTY_MINORS];
  366. static struct termios                 *serial_termios[SERIAL_TTY_MINORS];
  367. static struct termios                 *serial_termios_locked[SERIAL_TTY_MINORS];
  368. static struct usb_serial              *serial_table[SERIAL_TTY_MINORS]; /* initially all NULL */
  369.  
  370. LIST_HEAD(usb_serial_driver_list);
  371.  
  372. static struct usb_serial *get_serial_by_minor(int minor)
  373. {
  374.   return serial_table[minor];
  375. }
  376.  
  377. static struct usb_serial *get_free_serial(int num_ports, int *minor)
  378. {
  379.   struct usb_serial *serial = NULL;
  380.   int               i, j;
  381.   int               good_spot;
  382.  
  383.   dbg(__FUNCTION__ " %d", num_ports);
  384.  
  385.   *minor = 0;
  386.   for(i = 0; i < SERIAL_TTY_MINORS; ++i) {
  387.     if(serial_table[i])
  388.       continue;
  389.  
  390.     good_spot = 1;
  391.     for(j = 1; j <= num_ports - 1; ++j)
  392.       if(serial_table[i + j])
  393.         good_spot = 0;
  394.     if(good_spot == 0)
  395.       continue;
  396.  
  397.     if(!(serial = kmalloc(sizeof(struct usb_serial), GFP_KERNEL))) {
  398.       err(__FUNCTION__ " - Out of memory");
  399.       return NULL;
  400.     }
  401.  
  402.     memset(serial, 0, sizeof(struct usb_serial));
  403.     serial->magic = USB_SERIAL_MAGIC;
  404.     serial_table[i] = serial;
  405.     *minor = i;
  406.     dbg(__FUNCTION__ " - minor base = %d", *minor);
  407.     for(i = *minor + 1; (i < (*minor + num_ports)) && (i < SERIAL_TTY_MINORS); ++i)
  408.       serial_table[i] = serial;
  409.     return serial;
  410.   }
  411.  
  412.   return NULL;
  413. }
  414.  
  415. static void return_serial(struct usb_serial *serial)
  416. {
  417.   int i;
  418.  
  419.   dbg(__FUNCTION__);
  420.  
  421.   if(serial == NULL)
  422.     return;
  423.  
  424.   for(i = 0; i < serial->num_ports; ++i) {
  425.     serial_table[serial->minor + i] = NULL;
  426.   }
  427.  
  428.   return;
  429. }
  430.  
  431. #ifdef USES_EZUSB_FUNCTIONS
  432.  
  433. /* EZ-USB Control and Status Register.  Bit 0 controls 8051 reset */
  434. #define CPUCS_REG 0x7F92
  435.  
  436. int ezusb_writememory(struct usb_serial *serial, int address, unsigned char *data, int length, __u8 bRequest)
  437. {
  438.   int           result;
  439.   unsigned char *transfer_buffer = kmalloc(length, GFP_KERNEL);
  440.  
  441.   //    dbg("ezusb_writememory %x, %d", address, length);
  442.   if(!transfer_buffer) {
  443.     err(__FUNCTION__ " - kmalloc(%d) failed.", length);
  444.     return -ENOMEM;
  445.   }
  446.  
  447.   memcpy(transfer_buffer, data, length);
  448.   result = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0), bRequest, 0x40, address, 0, transfer_buffer,
  449.                            length, 300);
  450.   kfree(transfer_buffer);
  451.   return result;
  452. }
  453.  
  454. int ezusb_set_reset(struct usb_serial *serial, unsigned char reset_bit)
  455. {
  456.   int response;
  457.   dbg(__FUNCTION__ " - %d", reset_bit);
  458.   response = ezusb_writememory(serial, CPUCS_REG, &reset_bit, 1, 0xa0);
  459.   if(response < 0) {
  460.     err(__FUNCTION__ "- %d failed", reset_bit);
  461.   }
  462.  
  463.   return response;
  464. }
  465. #endif /* USES_EZUSB_FUNCTIONS */
  466.  
  467. /*****************************************************************************
  468.  * Driver tty interface functions
  469.  *****************************************************************************/
  470. static int serial_open(struct tty_struct *tty, struct file *filp)
  471. {
  472.   struct usb_serial       *serial;
  473.   struct usb_serial_port  *port;
  474.   int                     portNumber;
  475.  
  476.   dbg(__FUNCTION__);
  477.  
  478.   /* initialize the pointer incase something fails */
  479.   tty->driver_data = NULL;
  480.  
  481.   /* get the serial object associated with this tty pointer */
  482.   serial = get_serial_by_minor(MINOR(tty->device));
  483.  
  484.   if(serial_paranoia_check(serial, __FUNCTION__)) {
  485.     return -ENODEV;
  486.   }
  487.  
  488.   MOD_INC_USE_COUNT;
  489.  
  490.   /* set up our port structure making the tty driver remember our port object, and us it */
  491.   portNumber = MINOR(tty->device) - serial->minor;
  492.   port = &serial->port[portNumber];
  493.   tty->driver_data = port;
  494.   port->tty = tty;
  495.  
  496.   /* pass on to the driver specific version of this function if it is available */
  497.   if(serial->type->open) {
  498.     return(serial->type->open(port, filp));
  499.   }
  500.   else {
  501.     return(generic_open(port, filp));
  502.   }
  503. }
  504.  
  505. static void serial_close(struct tty_struct *tty, struct file *filp)
  506. {
  507.   struct usb_serial_port  *port = (struct usb_serial_port *)tty->driver_data;
  508.   struct usb_serial       *serial = get_usb_serial(port, __FUNCTION__);
  509.  
  510.   if(!serial) {
  511.     return;
  512.   }
  513.  
  514.   dbg(__FUNCTION__ " - port %d", port->number);
  515.  
  516.   if(!port->active) {
  517.     dbg(__FUNCTION__ " - port not opened");
  518.     return;
  519.   }
  520.  
  521.   /* pass on to the driver specific version of this function if it is available */
  522.   if(serial->type->close) {
  523.     serial->type->close(port, filp);
  524.   }
  525.   else {
  526.     generic_close(port, filp);
  527.   }
  528.  
  529.   MOD_DEC_USE_COUNT;
  530. }
  531.  
  532. static int serial_write(struct tty_struct *tty, int from_user, const unsigned char *buf, int count)
  533. {
  534.   struct usb_serial_port  *port = (struct usb_serial_port *)tty->driver_data;
  535.   struct usb_serial       *serial = get_usb_serial(port, __FUNCTION__);
  536.  
  537.   if(!serial) {
  538.     return -ENODEV;
  539.   }
  540.  
  541.   dbg(__FUNCTION__ " - port %d, %d byte(s)", port->number, count);
  542.  
  543.   if(!port->active) {
  544.     dbg(__FUNCTION__ " - port not opened");
  545.     return -EINVAL;
  546.   }
  547.  
  548.   /* pass on to the driver specific version of this function if it is available */
  549.   if(serial->type->write) {
  550.     return(serial->type->write(port, from_user, buf, count));
  551.   }
  552.   else {
  553.     return(generic_write(port, from_user, buf, count));
  554.   }
  555. }
  556.  
  557. static int serial_write_room(struct tty_struct *tty)
  558. {
  559.   struct usb_serial_port  *port = (struct usb_serial_port *)tty->driver_data;
  560.   struct usb_serial       *serial = get_usb_serial(port, __FUNCTION__);
  561.  
  562.   if(!serial) {
  563.     return -ENODEV;
  564.   }
  565.  
  566.   dbg(__FUNCTION__ " - port %d", port->number);
  567.  
  568.   if(!port->active) {
  569.     dbg(__FUNCTION__ " - port not open");
  570.     return -EINVAL;
  571.   }
  572.  
  573.   /* pass on to the driver specific version of this function if it is available */
  574.   if(serial->type->write_room) {
  575.     return(serial->type->write_room(port));
  576.   }
  577.   else {
  578.     return(generic_write_room(port));
  579.   }
  580. }
  581.  
  582. static int serial_chars_in_buffer(struct tty_struct *tty)
  583. {
  584.   struct usb_serial_port  *port = (struct usb_serial_port *)tty->driver_data;
  585.   struct usb_serial       *serial = get_usb_serial(port, __FUNCTION__);
  586.  
  587.   if(!serial) {
  588.     return -ENODEV;
  589.   }
  590.  
  591.   if(!port->active) {
  592.     dbg(__FUNCTION__ " - port not open");
  593.     return -EINVAL;
  594.   }
  595.  
  596.   /* pass on to the driver specific version of this function if it is available */
  597.   if(serial->type->chars_in_buffer) {
  598.     return(serial->type->chars_in_buffer(port));
  599.   }
  600.   else {
  601.     return(generic_chars_in_buffer(port));
  602.   }
  603. }
  604.  
  605. static void serial_throttle(struct tty_struct *tty)
  606. {
  607.   struct usb_serial_port  *port = (struct usb_serial_port *)tty->driver_data;
  608.   struct usb_serial       *serial = get_usb_serial(port, __FUNCTION__);
  609.  
  610.   if(!serial) {
  611.     return;
  612.   }
  613.  
  614.   dbg(__FUNCTION__ " - port %d", port->number);
  615.  
  616.   if(!port->active) {
  617.     dbg(__FUNCTION__ " - port not open");
  618.     return;
  619.   }
  620.  
  621.   /* pass on to the driver specific version of this function */
  622.   if(serial->type->throttle) {
  623.     serial->type->throttle(port);
  624.   }
  625.  
  626.   return;
  627. }
  628.  
  629. static void serial_unthrottle(struct tty_struct *tty)
  630. {
  631.   struct usb_serial_port  *port = (struct usb_serial_port *)tty->driver_data;
  632.   struct usb_serial       *serial = get_usb_serial(port, __FUNCTION__);
  633.  
  634.   if(!serial) {
  635.     return;
  636.   }
  637.  
  638.   dbg(__FUNCTION__ " - port %d", port->number);
  639.  
  640.   if(!port->active) {
  641.     dbg(__FUNCTION__ " - port not open");
  642.     return;
  643.   }
  644.  
  645.   /* pass on to the driver specific version of this function */
  646.   if(serial->type->unthrottle) {
  647.     serial->type->unthrottle(port);
  648.   }
  649.  
  650.   return;
  651. }
  652.  
  653. static int serial_ioctl(struct tty_struct *tty, struct file *file, unsigned int cmd, unsigned long arg)
  654. {
  655.   struct usb_serial_port  *port = (struct usb_serial_port *)tty->driver_data;
  656.   struct usb_serial       *serial = get_usb_serial(port, __FUNCTION__);
  657.  
  658.   if(!serial) {
  659.     return -ENODEV;
  660.   }
  661.  
  662.   dbg(__FUNCTION__ " - port %d, cmd 0x%.4x", port->number, cmd);
  663.  
  664.   if(!port->active) {
  665.     dbg(__FUNCTION__ " - port not open");
  666.     return -ENODEV;
  667.   }
  668.  
  669.   /* pass on to the driver specific version of this function if it is available */
  670.   if(serial->type->ioctl) {
  671.     return(serial->type->ioctl(port, file, cmd, arg));
  672.   }
  673.   else {
  674.     return -ENOIOCTLCMD;
  675.   }
  676. }
  677.  
  678. static void serial_set_termios(struct tty_struct *tty, struct termios *old)
  679. {
  680.   struct usb_serial_port  *port = (struct usb_serial_port *)tty->driver_data;
  681.   struct usb_serial       *serial = get_usb_serial(port, __FUNCTION__);
  682.  
  683.   if(!serial) {
  684.     return;
  685.   }
  686.  
  687.   dbg(__FUNCTION__ " - port %d", port->number);
  688.  
  689.   if(!port->active) {
  690.     dbg(__FUNCTION__ " - port not open");
  691.     return;
  692.   }
  693.  
  694.   /* pass on to the driver specific version of this function if it is available */
  695.   if(serial->type->set_termios) {
  696.     serial->type->set_termios(port, old);
  697.   }
  698.  
  699.   return;
  700. }
  701.  
  702. static void serial_break(struct tty_struct *tty, int break_state)
  703. {
  704.   struct usb_serial_port  *port = (struct usb_serial_port *)tty->driver_data;
  705.   struct usb_serial       *serial = get_usb_serial(port, __FUNCTION__);
  706.  
  707.   if(!serial) {
  708.     return;
  709.   }
  710.  
  711.   dbg(__FUNCTION__ " - port %d", port->number);
  712.  
  713.   if(!port->active) {
  714.     dbg(__FUNCTION__ " - port not open");
  715.     return;
  716.   }
  717.  
  718.   /* pass on to the driver specific version of this function if it is
  719.            available */
  720.   if(serial->type->break_ctl) {
  721.     serial->type->break_ctl(port, break_state);
  722.   }
  723. }
  724.  
  725. /*****************************************************************************
  726.  * generic devices specific driver functions
  727.  *****************************************************************************/
  728. static int generic_open(struct usb_serial_port *port, struct file *filp)
  729. {
  730.   struct usb_serial *serial = port->serial;
  731.   unsigned long     flags;
  732.   int               result;
  733.  
  734.   if(port_paranoia_check(port, __FUNCTION__))
  735.     return -ENODEV;
  736.  
  737.   MOD_INC_USE_COUNT;
  738.  
  739.   dbg(__FUNCTION__ " - port %d", port->number);
  740.  
  741.   spin_lock_irqsave(&port->port_lock, flags);
  742.  
  743.   ++port->open_count;
  744.  
  745.   if(!port->active) {
  746.     port->active = 1;
  747.  
  748.     /* force low_latency on so that our tty_push actually forces the data through, 
  749.            otherwise it is scheduled, and with high data rates (like with OHCI) data
  750.            can get lost. */
  751.     port->tty->low_latency = 1;
  752.  
  753.     /* if we have a bulk interrupt, start reading from it */
  754.     if(serial->num_bulk_in) {
  755.       /* Start reading from the device */
  756.       FILL_BULK_URB(port->read_urb, serial->dev, usb_rcvbulkpipe(serial->dev, port->bulk_in_endpointAddress),
  757.                     port->read_urb->transfer_buffer, port->read_urb->transfer_buffer_length,
  758.                     ((serial->type->read_bulk_callback) ? serial->type->read_bulk_callback : generic_read_bulk_callback),
  759.                     port);
  760.       result = usb_submit_urb(port->read_urb);
  761.       if(result)
  762.         err(__FUNCTION__ " - failed resubmitting read urb, error %d", result);
  763.     }
  764.   }
  765.  
  766.   spin_unlock_irqrestore(&port->port_lock, flags);
  767.  
  768.   return 0;
  769. }
  770.  
  771. static void generic_close(struct usb_serial_port *port, struct file *filp)
  772. {
  773.   struct usb_serial *serial = port->serial;
  774.   unsigned long     flags;
  775.  
  776.   dbg(__FUNCTION__ " - port %d", port->number);
  777.  
  778.   spin_lock_irqsave(&port->port_lock, flags);
  779.  
  780.   --port->open_count;
  781.  
  782.   if(port->open_count <= 0) {
  783.     /* shutdown any bulk reads that might be going on */
  784.     if(serial->num_bulk_out)
  785.       usb_unlink_urb(port->write_urb);
  786.     if(serial->num_bulk_in)
  787.       usb_unlink_urb(port->read_urb);
  788.  
  789.     port->active = 0;
  790.     port->open_count = 0;
  791.   }
  792.  
  793.   spin_unlock_irqrestore(&port->port_lock, flags);
  794.   MOD_DEC_USE_COUNT;
  795. }
  796.  
  797. static int generic_write(struct usb_serial_port *port, int from_user, const unsigned char *buf, int count)
  798. {
  799.   struct usb_serial *serial = port->serial;
  800.   unsigned long     flags;
  801.   int               result;
  802.  
  803.   dbg(__FUNCTION__ " - port %d", port->number);
  804.  
  805.   if(count == 0) {
  806.     dbg(__FUNCTION__ " - write request of 0 bytes");
  807.     return(0);
  808.   }
  809.  
  810.   /* only do something if we have a bulk out endpoint */
  811.   if(serial->num_bulk_out) {
  812.     if(port->write_urb->status == -EINPROGRESS) {
  813.       dbg(__FUNCTION__ " - already writing");
  814.       return(0);
  815.     }
  816.  
  817.     spin_lock_irqsave(&port->port_lock, flags);
  818.     count = (count > port->bulk_out_size) ? port->bulk_out_size : count;
  819.  
  820.     usb_serial_debug_data(__FILE__, __FUNCTION__, count, buf);
  821.  
  822.     if(from_user) {
  823.       copy_from_user(port->write_urb->transfer_buffer, buf, count);
  824.     }
  825.     else {
  826.       memcpy(port->write_urb->transfer_buffer, buf, count);
  827.     }
  828.  
  829.     /* set up our urb */
  830.     FILL_BULK_URB(port->write_urb, serial->dev, usb_sndbulkpipe(serial->dev, port->bulk_out_endpointAddress),
  831.                   port->write_urb->transfer_buffer, count,
  832.                   ((serial->type->write_bulk_callback) ? serial->type->write_bulk_callback : generic_write_bulk_callback),
  833.                   port);
  834.  
  835.     /* send the data out the bulk port */
  836.     result = usb_submit_urb(port->write_urb);
  837.     if(result) {
  838.       err(__FUNCTION__ " - failed submitting write urb, error %d", result);
  839.       spin_unlock_irqrestore(&port->port_lock, flags);
  840.       return 0;
  841.     }
  842.  
  843.     spin_unlock_irqrestore(&port->port_lock, flags);
  844.     return(count);
  845.   }
  846.  
  847.   /* no bulk out, so return 0 bytes written */
  848.   return(0);
  849. }
  850.  
  851. static int generic_write_room(struct usb_serial_port *port)
  852. {
  853.   struct usb_serial *serial = port->serial;
  854.   int               room = 0;
  855.  
  856.   dbg(__FUNCTION__ " - port %d", port->number);
  857.  
  858.   if(serial->num_bulk_out)
  859.     if(port->write_urb->status != -EINPROGRESS)
  860.       room = port->bulk_out_size;
  861.  
  862.   dbg(__FUNCTION__ " - returns %d", room);
  863.   return(room);
  864. }
  865.  
  866. static int generic_chars_in_buffer(struct usb_serial_port *port)
  867. {
  868.   struct usb_serial *serial = port->serial;
  869.   int               chars = 0;
  870.  
  871.   dbg(__FUNCTION__ " - port %d", port->number);
  872.  
  873.   if(serial->num_bulk_out)
  874.     if(port->write_urb->status == -EINPROGRESS)
  875.       chars = port->write_urb->transfer_buffer_length;
  876.  
  877.   dbg(__FUNCTION__ " - returns %d", chars);
  878.   return(chars);
  879. }
  880.  
  881. static void generic_read_bulk_callback(struct urb *urb)
  882. {
  883.   struct usb_serial_port  *port = (struct usb_serial_port *)urb->context;
  884.   struct usb_serial       *serial = get_usb_serial(port, __FUNCTION__);
  885.   struct tty_struct       *tty;
  886.   unsigned char           *data = urb->transfer_buffer;
  887.   int                     i;
  888.   int                     result;
  889.  
  890.   dbg(__FUNCTION__ " - port %d", port->number);
  891.  
  892.   if(!serial) {
  893.     dbg(__FUNCTION__ " - bad serial pointer, exiting");
  894.     return;
  895.   }
  896.  
  897.   if(urb->status) {
  898.     dbg(__FUNCTION__ " - nonzero read bulk status received: %d", urb->status);
  899.     return;
  900.   }
  901.  
  902.   usb_serial_debug_data(__FILE__, __FUNCTION__, urb->actual_length, data);
  903.  
  904.   tty = port->tty;
  905.   if(urb->actual_length) {
  906.     for(i = 0; i < urb->actual_length; ++i) {
  907.       /* if we insert more than TTY_FLIPBUF_SIZE characters, we drop them. */
  908.       if(tty->flip.count >= TTY_FLIPBUF_SIZE) {
  909.         tty_flip_buffer_push(tty);
  910.       }
  911.  
  912.       /* this doesn't actually push the data through unless tty->low_latency is set */
  913.       tty_insert_flip_char(tty, data[i], 0);
  914.     }
  915.  
  916.     tty_flip_buffer_push(tty);
  917.   }
  918.  
  919.   /* Continue trying to always read  */
  920.   FILL_BULK_URB(port->read_urb, serial->dev, usb_rcvbulkpipe(serial->dev, port->bulk_in_endpointAddress),
  921.                 port->read_urb->transfer_buffer, port->read_urb->transfer_buffer_length,
  922.                 ((serial->type->read_bulk_callback) ? serial->type->read_bulk_callback : generic_read_bulk_callback),
  923.                 port);
  924.   result = usb_submit_urb(port->read_urb);
  925.   if(result)
  926.     err(__FUNCTION__ " - failed resubmitting read urb, error %d", result);
  927. }
  928.  
  929. static void generic_write_bulk_callback(struct urb *urb)
  930. {
  931.   struct usb_serial_port  *port = (struct usb_serial_port *)urb->context;
  932.   struct usb_serial       *serial = get_usb_serial(port, __FUNCTION__);
  933.  
  934.   dbg(__FUNCTION__ " - port %d", port->number);
  935.  
  936.   if(!serial) {
  937.     dbg(__FUNCTION__ " - bad serial pointer, exiting");
  938.     return;
  939.   }
  940.  
  941.   if(urb->status) {
  942.     dbg(__FUNCTION__ " - nonzero write bulk status received: %d", urb->status);
  943.     return;
  944.   }
  945.  
  946.   queue_task(&port->tqueue, &tq_immediate);
  947.   mark_bh(IMMEDIATE_BH);
  948.  
  949.   return;
  950. }
  951.  
  952. #ifdef CONFIG_USB_SERIAL_GENERIC
  953. static void generic_shutdown(struct usb_serial *serial)
  954. {
  955.   int i;
  956.  
  957.   dbg(__FUNCTION__);
  958.  
  959.   /* stop reads and writes on all ports */
  960.   for(i = 0; i < serial->num_ports; ++i) {
  961.     while(serial->port[i].open_count > 0) {
  962.       generic_close(&serial->port[i], NULL);
  963.     }
  964.   }
  965. }
  966. #endif
  967. static void port_softint(void *private)
  968. {
  969.   struct usb_serial_port  *port = (struct usb_serial_port *)private;
  970.   struct usb_serial       *serial = get_usb_serial(port, __FUNCTION__);
  971.   struct tty_struct       *tty;
  972.  
  973.   dbg(__FUNCTION__ " - port %d", port->number);
  974.  
  975.   if(!serial) {
  976.     return;
  977.   }
  978.  
  979.   tty = port->tty;
  980.   if((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) && tty->ldisc.write_wakeup) {
  981.     dbg(__FUNCTION__ " - write wakeup call.");
  982.     (tty->ldisc.write_wakeup) (tty);
  983.   }
  984.  
  985.   wake_up_interruptible(&tty->write_wait);
  986. }
  987.  
  988. static void *usb_serial_probe(struct usb_device *dev, unsigned int ifnum, const struct usb_device_id *id)
  989. {
  990.   struct usb_serial               *serial = NULL;
  991.   struct usb_serial_port          *port;
  992.   struct usb_interface            *interface;
  993.   struct usb_interface_descriptor *iface_desc;
  994.   struct usb_endpoint_descriptor  *endpoint;
  995.   struct usb_endpoint_descriptor  *interrupt_in_endpoint[MAX_NUM_PORTS];
  996.   struct usb_endpoint_descriptor  *bulk_in_endpoint[MAX_NUM_PORTS];
  997.   struct usb_endpoint_descriptor  *bulk_out_endpoint[MAX_NUM_PORTS];
  998.   struct usb_serial_device_type   *type = NULL;
  999.   struct list_head                *tmp;
  1000.   int                             found;
  1001.   int                             minor;
  1002.   int                             buffer_size;
  1003.   int                             i;
  1004.   char                            interrupt_pipe;
  1005.   char                            bulk_in_pipe;
  1006.   char                            bulk_out_pipe;
  1007.   int                             num_interrupt_in = 0;
  1008.   int                             num_bulk_in = 0;
  1009.   int                             num_bulk_out = 0;
  1010.   int                             num_ports;
  1011.   int                             max_endpoints;
  1012.   const struct usb_device_id      *id_pattern = NULL;
  1013.  
  1014.   /* loop through our list of known serial converters, and see if this
  1015.        device matches. */
  1016.   found = 0;
  1017.   interface = &dev->actconfig->interface[ifnum];
  1018.   list_for_each(tmp, &usb_serial_driver_list) {
  1019.     type = list_entry(tmp, struct usb_serial_device_type, driver_list);
  1020.     id_pattern = usb_match_id(dev, interface, type->id_table);
  1021.     if(id_pattern != NULL) {
  1022.       dbg("descriptor matches");
  1023.       found = 1;
  1024.       break;
  1025.     }
  1026.   }
  1027.  
  1028.   if(!found) {
  1029.     /* no match */
  1030.     dbg("none matched");
  1031.     return(NULL);
  1032.   }
  1033.  
  1034.   /* descriptor matches, let's find the endpoints needed */
  1035.   interrupt_pipe = bulk_in_pipe = bulk_out_pipe = HAS_NOT;
  1036.  
  1037.   /* check out the endpoints */
  1038.   iface_desc = &interface->altsetting[0];
  1039.   for(i = 0; i < iface_desc->bNumEndpoints; ++i) {
  1040.     endpoint = &iface_desc->endpoint[i];
  1041.  
  1042.     if((endpoint->bEndpointAddress & 0x80) && ((endpoint->bmAttributes & 3) == 0x02)) {
  1043.       /* we found a bulk in endpoint */
  1044.       dbg("found bulk in");
  1045.       bulk_in_pipe = HAS;
  1046.       bulk_in_endpoint[num_bulk_in] = endpoint;
  1047.       ++num_bulk_in;
  1048.     }
  1049.  
  1050.     if(((endpoint->bEndpointAddress & 0x80) == 0x00) && ((endpoint->bmAttributes & 3) == 0x02)) {
  1051.       /* we found a bulk out endpoint */
  1052.       dbg("found bulk out");
  1053.       bulk_out_pipe = HAS;
  1054.       bulk_out_endpoint[num_bulk_out] = endpoint;
  1055.       ++num_bulk_out;
  1056.     }
  1057.  
  1058.     if((endpoint->bEndpointAddress & 0x80) && ((endpoint->bmAttributes & 3) == 0x03)) {
  1059.       /* we found a interrupt in endpoint */
  1060.       dbg("found interrupt in");
  1061.       interrupt_pipe = HAS;
  1062.       interrupt_in_endpoint[num_interrupt_in] = endpoint;
  1063.       ++num_interrupt_in;
  1064.     }
  1065.   }
  1066.  
  1067.   /* verify that we found all of the endpoints that we need */
  1068.   if
  1069.   (
  1070.     !(
  1071.       (interrupt_pipe & type->needs_interrupt_in) &&
  1072.       (bulk_in_pipe & type->needs_bulk_in) &&
  1073.       (bulk_out_pipe & type->needs_bulk_out)
  1074.     )
  1075.   ) {
  1076.     /* nope, they don't match what we expected */
  1077.     info("descriptors matched, but endpoints did not");
  1078.     return NULL;
  1079.   }
  1080.  
  1081.   /* found all that we need */
  1082.   info("%s converter detected", type->name);
  1083.  
  1084. #ifdef CONFIG_USB_SERIAL_GENERIC
  1085.   if(type == &generic_device) {
  1086.     num_ports = num_bulk_out;
  1087.     if(num_ports == 0) {
  1088.       err("Generic device with no bulk out, not allowed.");
  1089.       return NULL;
  1090.     }
  1091.   }
  1092.   else
  1093. #endif
  1094.     num_ports = type->num_ports;
  1095.  
  1096.   serial = get_free_serial(num_ports, &minor);
  1097.   if(serial == NULL) {
  1098.     err("No more free serial devices");
  1099.     return NULL;
  1100.   }
  1101.  
  1102.   serial->dev = dev;
  1103.   serial->type = type;
  1104.   serial->interface = interface;
  1105.   serial->minor = minor;
  1106.   serial->num_ports = num_ports;
  1107.   serial->num_bulk_in = num_bulk_in;
  1108.   serial->num_bulk_out = num_bulk_out;
  1109.   serial->num_interrupt_in = num_interrupt_in;
  1110.  
  1111.   /* if this device type has a startup function, call it */
  1112.   if(type->startup) {
  1113.     if(type->startup(serial)) {
  1114.       goto probe_error;
  1115.     }
  1116.   }
  1117.  
  1118.   /* set up the endpoint information */
  1119.   for(i = 0; i < num_bulk_in; ++i) {
  1120.     endpoint = bulk_in_endpoint[i];
  1121.     port = &serial->port[i];
  1122.     port->read_urb = usb_alloc_urb(0);
  1123.     if(!port->read_urb) {
  1124.       err("No free urbs available");
  1125.       goto probe_error;
  1126.     }
  1127.  
  1128.     buffer_size = endpoint->wMaxPacketSize;
  1129.     port->bulk_in_endpointAddress = endpoint->bEndpointAddress;
  1130.     port->bulk_in_buffer = kmalloc(buffer_size, GFP_KERNEL);
  1131.     if(!port->bulk_in_buffer) {
  1132.       err("Couldn't allocate bulk_in_buffer");
  1133.       goto probe_error;
  1134.     }
  1135.  
  1136.     FILL_BULK_URB(port->read_urb, dev, usb_rcvbulkpipe(dev, endpoint->bEndpointAddress), port->bulk_in_buffer,
  1137.                   buffer_size,
  1138.                   ((serial->type->read_bulk_callback) ? serial->type->read_bulk_callback : generic_read_bulk_callback),
  1139.                   port);
  1140.   }
  1141.  
  1142.   for(i = 0; i < num_bulk_out; ++i) {
  1143.     endpoint = bulk_out_endpoint[i];
  1144.     port = &serial->port[i];
  1145.     port->write_urb = usb_alloc_urb(0);
  1146.     if(!port->write_urb) {
  1147.       err("No free urbs available");
  1148.       goto probe_error;
  1149.     }
  1150.  
  1151.     buffer_size = endpoint->wMaxPacketSize;
  1152.     port->bulk_out_size = buffer_size;
  1153.     port->bulk_out_endpointAddress = endpoint->bEndpointAddress;
  1154.     port->bulk_out_buffer = kmalloc(buffer_size, GFP_KERNEL);
  1155.     if(!port->bulk_out_buffer) {
  1156.       err("Couldn't allocate bulk_out_buffer");
  1157.       goto probe_error;
  1158.     }
  1159.  
  1160.     FILL_BULK_URB(port->write_urb, dev, usb_sndbulkpipe(dev, endpoint->bEndpointAddress), port->bulk_out_buffer,
  1161.                   buffer_size,
  1162.                   ((serial->type->write_bulk_callback) ? serial->type->write_bulk_callback : generic_write_bulk_callback),
  1163.                   port);
  1164.   }
  1165.  
  1166.   for(i = 0; i < num_interrupt_in; ++i) {
  1167.     endpoint = interrupt_in_endpoint[i];
  1168.     port = &serial->port[i];
  1169.     port->interrupt_in_urb = usb_alloc_urb(0);
  1170.     if(!port->interrupt_in_urb) {
  1171.       err("No free urbs available");
  1172.       goto probe_error;
  1173.     }
  1174.  
  1175.     buffer_size = endpoint->wMaxPacketSize;
  1176.     port->interrupt_in_endpointAddress = endpoint->bEndpointAddress;
  1177.     port->interrupt_in_buffer = kmalloc(buffer_size, GFP_KERNEL);
  1178.     if(!port->interrupt_in_buffer) {
  1179.       err("Couldn't allocate interrupt_in_buffer");
  1180.       goto probe_error;
  1181.     }
  1182.  
  1183.     FILL_INT_URB(port->interrupt_in_urb, dev, usb_rcvintpipe(dev, endpoint->bEndpointAddress), port->interrupt_in_buffer,
  1184.                  buffer_size, serial->type->read_int_callback, port, endpoint->bInterval);
  1185.   }
  1186.  
  1187.   /* initialize some parts of the port structures */
  1188.  
  1189.   /* we don't use num_ports here cauz some devices have more endpoint pairs than ports */
  1190.   max_endpoints = MAX(num_bulk_in, num_bulk_out);
  1191.   max_endpoints = MAX(max_endpoints, num_interrupt_in);
  1192.   dbg(__FUNCTION__ " - setting up %d port structures for this device", max_endpoints);
  1193.   for(i = 0; i < max_endpoints; ++i) {
  1194.     port = &serial->port[i];
  1195.     port->number = i + serial->minor;
  1196.     port->serial = serial;
  1197.     port->magic = USB_SERIAL_PORT_MAGIC;
  1198.     port->tqueue.routine = port_softint;
  1199.     port->tqueue.data = port;
  1200.     spin_lock_init(&port->port_lock);
  1201.   }
  1202.  
  1203.   /* initialize the devfs nodes for this device and let the user know what ports we are bound to */
  1204.   for(i = 0; i < serial->num_ports; ++i) {
  1205.     tty_register_devfs(&serial_tty_driver, 0, serial->port[i].number);
  1206.     info("%s converter now attached to ttyUSB%d (or usb/tts/%d for devfs)", type->name, serial->port[i].number,
  1207.          serial->port[i].number);
  1208.   }
  1209.  
  1210.   return serial;  /* success */
  1211.  
  1212. probe_error:
  1213.   for(i = 0; i < num_bulk_in; ++i) {
  1214.     port = &serial->port[i];
  1215.     if(port->read_urb)
  1216.       usb_free_urb(port->read_urb);
  1217.     if(port->bulk_in_buffer)
  1218.       kfree(port->bulk_in_buffer);
  1219.   }
  1220.  
  1221.   for(i = 0; i < num_bulk_out; ++i) {
  1222.     port = &serial->port[i];
  1223.     if(port->write_urb)
  1224.       usb_free_urb(port->write_urb);
  1225.     if(port->bulk_out_buffer)
  1226.       kfree(port->bulk_out_buffer);
  1227.   }
  1228.  
  1229.   for(i = 0; i < num_interrupt_in; ++i) {
  1230.     port = &serial->port[i];
  1231.     if(port->interrupt_in_urb)
  1232.       usb_free_urb(port->interrupt_in_urb);
  1233.     if(port->interrupt_in_buffer)
  1234.       kfree(port->interrupt_in_buffer);
  1235.   }
  1236.  
  1237.   /* return the minor range that this device had */
  1238.   return_serial(serial);
  1239.  
  1240.   /* free up any memory that we allocated */
  1241.   kfree(serial);
  1242.   return NULL;
  1243. }
  1244.  
  1245. static void usb_serial_disconnect(struct usb_device *dev, void *ptr)
  1246. {
  1247.   struct usb_serial       *serial = (struct usb_serial *)ptr;
  1248.   struct usb_serial_port  *port;
  1249.   int                     i;
  1250.  
  1251.   if(serial) {
  1252.     /* fail all future close/read/write/ioctl/etc calls */
  1253.     for(i = 0; i < serial->num_ports; ++i) {
  1254.       if(serial->port[i].tty != NULL)
  1255.         serial->port[i].tty->driver_data = NULL;
  1256.     }
  1257.  
  1258.     if(serial->type->shutdown)
  1259.       serial->type->shutdown(serial);
  1260.  
  1261.     for(i = 0; i < serial->num_ports; ++i)
  1262.       serial->port[i].active = 0;
  1263.  
  1264.     for(i = 0; i < serial->num_bulk_in; ++i) {
  1265.       port = &serial->port[i];
  1266.       if(port->read_urb) {
  1267.         usb_unlink_urb(port->read_urb);
  1268.         usb_free_urb(port->read_urb);
  1269.       }
  1270.  
  1271.       if(port->bulk_in_buffer)
  1272.         kfree(port->bulk_in_buffer);
  1273.     }
  1274.  
  1275.     for(i = 0; i < serial->num_bulk_out; ++i) {
  1276.       port = &serial->port[i];
  1277.       if(port->write_urb) {
  1278.         usb_unlink_urb(port->write_urb);
  1279.         usb_free_urb(port->write_urb);
  1280.       }
  1281.  
  1282.       if(port->bulk_out_buffer)
  1283.         kfree(port->bulk_out_buffer);
  1284.     }
  1285.  
  1286.     for(i = 0; i < serial->num_interrupt_in; ++i) {
  1287.       port = &serial->port[i];
  1288.       if(port->interrupt_in_urb) {
  1289.         usb_unlink_urb(port->interrupt_in_urb);
  1290.         usb_free_urb(port->interrupt_in_urb);
  1291.       }
  1292.  
  1293.       if(port->interrupt_in_buffer)
  1294.         kfree(port->interrupt_in_buffer);
  1295.     }
  1296.  
  1297.     for(i = 0; i < serial->num_ports; ++i) {
  1298.       tty_unregister_devfs(&serial_tty_driver, serial->port[i].number);
  1299.       info("%s converter now disconnected from ttyUSB%d", serial->type->name, serial->port[i].number);
  1300.     }
  1301.  
  1302.     /* return the minor range that this device had */
  1303.     return_serial(serial);
  1304.  
  1305.     /* free up any memory that we allocated */
  1306.     kfree(serial);
  1307.   }
  1308.   else {
  1309.     info("device disconnected");
  1310.   }
  1311. }
  1312.  
  1313. static struct tty_driver  serial_tty_driver =
  1314. {
  1315. magic:
  1316.   TTY_DRIVER_MAGIC,
  1317.   driver_name : "usb-serial",
  1318.   name : "usb/tts/%d",
  1319.   major : SERIAL_TTY_MAJOR,
  1320.   minor_start : 0,
  1321.   num : SERIAL_TTY_MINORS,
  1322.   type : TTY_DRIVER_TYPE_SERIAL,
  1323.   subtype : SERIAL_TYPE_NORMAL,
  1324.   flags : TTY_DRIVER_REAL_RAW | TTY_DRIVER_NO_DEVFS,
  1325.   refcount : &serial_refcount,
  1326.   table : serial_tty,
  1327.   termios : serial_termios,
  1328.   termios_locked : serial_termios_locked,
  1329.   open : serial_open,
  1330.   close : serial_close,
  1331.   write : serial_write,
  1332.   write_room : serial_write_room,
  1333.   ioctl : serial_ioctl,
  1334.   set_termios : serial_set_termios,
  1335.   throttle : serial_throttle,
  1336.   unthrottle : serial_unthrottle,
  1337.   break_ctl : serial_break,
  1338.   chars_in_buffer : serial_chars_in_buffer,
  1339. };
  1340.  
  1341. int usb_serial_init(void)
  1342. {
  1343.   int i;
  1344.   int result;
  1345.  
  1346.   /* Initalize our global data */
  1347.   for(i = 0; i < SERIAL_TTY_MINORS; ++i) {
  1348.     serial_table[i] = NULL;
  1349.   }
  1350.  
  1351.   /* register the tty driver */
  1352.   serial_tty_driver.init_termios = tty_std_termios;
  1353.   serial_tty_driver.init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
  1354.   if(tty_register_driver(&serial_tty_driver)) {
  1355.     err(__FUNCTION__ " - failed to register tty driver");
  1356.     return -1;
  1357.   }
  1358.  
  1359. #ifdef CONFIG_USB_SERIAL_GENERIC
  1360.   generic_device_ids[0].idVendor = vendor;
  1361.   generic_device_ids[0].idProduct = product;
  1362.   generic_device_ids[0].match_flags = USB_DEVICE_ID_MATCH_VENDOR | USB_DEVICE_ID_MATCH_PRODUCT;
  1363.  
  1364.   /* register our generic driver with ourselves */
  1365.   usb_serial_register(&generic_device);
  1366. #endif
  1367.   /* register the USB driver */
  1368.   result = usb_register(&usb_serial_driver);
  1369.   if(result < 0) {
  1370.     tty_unregister_driver(&serial_tty_driver);
  1371.     err("usb_register failed for the usb-serial driver. Error number %d", result);
  1372.     return -1;
  1373.   }
  1374.  
  1375.   return 0;
  1376. }
  1377.  
  1378. void usb_serial_exit(void)
  1379. {
  1380. #ifdef CONFIG_USB_SERIAL_GENERIC
  1381.  
  1382.   /* remove our generic driver */
  1383.   usb_serial_deregister(&generic_device);
  1384. #endif
  1385.   usb_deregister(&usb_serial_driver);
  1386.   tty_unregister_driver(&serial_tty_driver);
  1387. }
  1388.  
  1389. module_init(usb_serial_init);
  1390. module_exit(usb_serial_exit);
  1391.  
  1392. int usb_serial_register(struct usb_serial_device_type *new_device)
  1393. {
  1394.   /* Add this device to our list of devices */
  1395.   list_add(&new_device->driver_list, &usb_serial_driver_list);
  1396.  
  1397.   info("USB Serial support registered for %s", new_device->name);
  1398.  
  1399.   usb_scan_devices();
  1400.  
  1401.   return 0;
  1402. }
  1403.  
  1404. void usb_serial_deregister(struct usb_serial_device_type *device)
  1405. {
  1406.   struct usb_serial *serial;
  1407.   int               i;
  1408.  
  1409.   info("USB Serial deregistering driver %s", device->name);
  1410.  
  1411.   /* clear out the serial_table if the device is attached to a port */
  1412.   for(i = 0; i < SERIAL_TTY_MINORS; ++i) {
  1413.     serial = serial_table[i];
  1414.     if((serial != NULL) && (serial->type == device)) {
  1415.       usb_driver_release_interface(&usb_serial_driver, serial->interface);
  1416.       usb_serial_disconnect(NULL, serial);
  1417.     }
  1418.   }
  1419.  
  1420.   list_del(&device->driver_list);
  1421. }
  1422.  
  1423. /* If the usb-serial core is build into the core, the usb-serial drivers
  1424.    need these symbols to load properly as modules. */
  1425. EXPORT_SYMBOL(usb_serial_register);
  1426. EXPORT_SYMBOL(usb_serial_deregister);
  1427. #ifdef USES_EZUSB_FUNCTIONS
  1428. EXPORT_SYMBOL(ezusb_writememory);
  1429. EXPORT_SYMBOL(ezusb_set_reset);
  1430. #endif
  1431.